""" Models for storing logs from apps and services in AppScale. """

from google.appengine.ext import ndb

class AppLogLine(ndb.Model):
  """ A Datastore Model that represents a single log line sent by an AppScale
  service.

  Fields:
    message: A str containing the message that was logged.
    level: An integer (typically in the range [1, 5]) corresponding to the
      severity of this log message. Lower log levels correspond to less severe
      log messages.
    timestamp: The time that this log was generated by an AppScale service.
  """
  message = ndb.TextProperty()
  level = ndb.IntegerProperty(indexed=False)
  timestamp = ndb.DateTimeProperty(indexed=False)


class RequestLogLine(ndb.Model):
  """ A Datastore Model that represents all of the logs associated with a single
  web request.

  As not all services within AppScale serve traditional web traffic, a
  RequestLogLine may have only a single AppLogLine.

  Fields:
    service_name: The name of the service that generated this request log. In
      the case of Google App Engine applications, the service name is the
      application's ID.
    host: The hostname of the machine that generated this request log.
    app_logs: One or more AppLogLines that contain logs generated during this
      request.
    timestamp: datetime of when this entity was created.
  """
  service_name = ndb.StringProperty()
  host = ndb.StringProperty()
  app_logs = ndb.StructuredProperty(AppLogLine, repeated=True, indexed=False)
  timestamp = ndb.DateTimeProperty(auto_now_add=True, auto_now=True)

